package com.lxmes.device.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.lxmes.device.domain.DeviceFailureReport;
import com.lxmes.device.service.INcEmCygzService;
import com.lxmes.device.service.INcEmGcyzService;
import com.lxmes.device.service.INcEmGzwxService;
import com.lxmes.system.service.ISysDictDataService;
import com.lxmes.system.service.ISysPostService;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.device.domain.NcEmGzbx;
import com.lxmes.device.service.INcEmGzbxService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 设备故障报修Controller
 * 
 * @author ruoyi
 * @date 2023-02-11
 */
@RestController
@RequestMapping("/device/gzbx")
public class NcEmGzbxController extends BaseController
{
    @Autowired
    private INcEmGzbxService ncEmGzbxService;

    @Autowired
    private ISysDictDataService dataService;

    @Autowired
    private INcEmGzwxService ncEmGzwxService;

    @Autowired
    private ISysPostService postService;
    @Autowired
    private INcEmGcyzService ncEmGcyzService;
    /**
     * 查询设备故障报修列表
     */
    @PreAuthorize("@ss.hasPermi('device:gzbx:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcEmGzbx ncEmGzbx)
    {
        startPage();
        List<NcEmGzbx> list = ncEmGzbxService.selectNcEmGzbxList(ncEmGzbx);
        return getDataTable(list);
    }

    /**
     * 导出设备故障报修列表
     */
    @PreAuthorize("@ss.hasPermi('device:gzbx:export')")
    @Log(title = "设备故障报修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcEmGzbx ncEmGzbx)
    {
        List<NcEmGzbx> list = ncEmGzbxService.selectNcEmGzbxList(ncEmGzbx);
        ExcelUtil<NcEmGzbx> util = new ExcelUtil<NcEmGzbx>(NcEmGzbx.class);
        util.exportExcel(response, list, "设备故障报修数据");
    }

    /**
     * 获取设备故障报修详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:gzbx:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ncEmGzbxService.selectNcEmGzbxById(id));
    }

    /**
     * 新增设备故障报修
     */
    @PreAuthorize("@ss.hasPermi('device:gzbx:add')")
    @Log(title = "设备故障报修", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcEmGzbx ncEmGzbx) throws Exception {
        return toAjax(ncEmGzbxService.insertNcEmGzbx(ncEmGzbx));
    }

    /**
     * 修改设备故障报修
     */
    @PreAuthorize("@ss.hasPermi('device:gzbx:edit')")
    @Log(title = "设备故障报修", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcEmGzbx ncEmGzbx)
    {
        return toAjax(ncEmGzbxService.updateNcEmGzbx(ncEmGzbx));
    }

    /**
     * 删除设备故障报修
     */
    @PreAuthorize("@ss.hasPermi('device:gzbx:remove')")
    @Log(title = "设备故障报修", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ncEmGzbxService.deleteNcEmGzbxByIds(ids));
    }




    /**
     * 新增设备故障报修单
     */
    @ApiOperation(value="添加故障报修")
    @Log(title = "设备故障报修单", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('mobile:report:add')")
    @PostMapping("/apiAdd")
    public AjaxResult apiAdd(@RequestBody NcEmGzbx ncEmGzbx) throws Exception {
        return toAjax(ncEmGzbxService.insertNcEmGzbx(ncEmGzbx));
    }
    /**
     * 查询设备故障报修单列表
     */
    @ApiOperation(value="全部故障报修单列表",notes = "状态代表 0-待接单1-待执行2-维修中3-待评价4-完成")
    //查询条件
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryParams", value = "报修单号、设备编码、设备名称、报修人", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "faultType", value = "故障类别", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "faultProgress", value = "状态", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "faultUrgency", value = "紧急情况", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",  required = true,paramType="query",  dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "当前页数",  required = true,paramType="query",   dataType = "int", dataTypeClass = Integer.class)
    })
    @ApiResponses({@ApiResponse(code=200,message = "0k",response = DeviceFailureReport.class)})
    @PreAuthorize("@ss.hasPermi('mobile:report:list')")
    @GetMapping("/apiList")
    public TableDataInfo apiList(
            @RequestParam(value="queryParams",required = false) String queryParams,
            @RequestParam(value="faultType",required = false) String faultType,
            @RequestParam(value="faultProgress",required = false) String faultProgress,
            @RequestParam(value="faultUrgency",required = false) String faultUrgency
    )
    {
        NcEmGzbx ncEmGzbx=new NcEmGzbx();
        ncEmGzbx.setQueryParams(queryParams);
        ncEmGzbx.setGzlb(faultType);
        ncEmGzbx.setJjcd(faultUrgency);
        ncEmGzbx.setCljd(faultProgress);
        startPage();
        List<NcEmGzbx> list = ncEmGzbxService.selectNcEmGzbxList(ncEmGzbx);
        return getDataTable(list);
    }
    @ApiOperation(value="获取字典是否停机")
    @GetMapping("/getApiDictDataIsShutDown")
    public AjaxResult getDeviceIsFlag(){
        return AjaxResult.success( dataService.selectDictDataByType("device_is_shut_down"));
    }
    @ApiOperation(value="获取字典紧急程度")
    @GetMapping("/getApiDictDataFaultUrgency")
    public AjaxResult getApiDictDataFaultUrgency(){
        return AjaxResult.success( dataService.selectDictDataByType("device_fault_urgency"));
    }
    @ApiOperation(value="获取字典故障等级")
    @GetMapping("/getApiDictDataFaultLevel")
    public AjaxResult getApiDictDataFaultLevel(){
        return AjaxResult.success( dataService.selectDictDataByType("device_fault_level"));
    }
    @ApiOperation(value="获取字典故障类别")
    @GetMapping("/getApiDictDataFaultType")
    public AjaxResult getApiDictDataFaultType(){
        return AjaxResult.success( dataService.selectDictDataByType("device_fault_type"));
    }
    @ApiOperation(value="获取维修班组")
    @GetMapping("/getApiWxDept")
    public AjaxResult getApiWxDept(){
        return AjaxResult.success( postService.selectPostById(5L));
    }
    /*@ApiOperation(value="获取维修班组人员")
    @GetMapping("/getApiUserPostList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nickName", value = "用户名称", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",  required = true,paramType="query",  dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "当前页数",  required = true,paramType="query",   dataType = "int", dataTypeClass = Integer.class)
    })
    public TableDataInfo userPostList(@RequestParam(value="nickName",required = false) String nickName)
    {
        SysUser user=new SysUser();
        user.setNickName(nickName);
        startPage();
        List<SysUser> list = userService.selectUserPostList(user);
        return getDataTable(list);
    }*/

    @ApiOperation(value="取消报修单",notes = "只能是状态为0待处理状态下可取消")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportId", value = "报修单号", required = true,paramType="path", dataType = "String", dataTypeClass = String.class),
    })
    @PreAuthorize("@ss.hasPermi('mobile:report:delete')")
    @PostMapping(value = "/apiCancelFailureReport/{reportId}")
    public AjaxResult apiCancelFailureReport(@PathVariable("reportId") String reportId)
    {
        return toAjax(ncEmGzbxService.apiCancelFailureReport(reportId));
    }
    /**
     * API获取设备故障报修单详细信息
     */
    @ApiOperation(value="故障报修单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportId", value = "报修单号", required = true,paramType="path", dataType = "String", dataTypeClass = String.class),
    })
    @PreAuthorize("@ss.hasPermi('mobile:report:query')")
    @GetMapping(value = "/getReportInfo/{reportId}")
    public AjaxResult getReportInfo(@PathVariable("reportId") String reportId)
    {
        return AjaxResult.success(ncEmGzbxService.selectNcEmGzbxByReportId(reportId));
    }
    /**
     * API获取设备故障报修单详细信息
     */
    @ApiOperation(value="故障报修单维修单验证组合详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportId", value = "报修单号", required = true,paramType="path", dataType = "String", dataTypeClass = String.class),
    })
    @PreAuthorize("@ss.hasPermi('mobile:report:queryInfo')")
    @GetMapping(value = "/getReportAndRepairInfo/{reportId}")
    public AjaxResult getReportAndRepairInfo(@PathVariable("reportId") String reportId)
    {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("report",ncEmGzbxService.selectNcEmGzbxByReportId(reportId));
        jsonObject.put("repair", ncEmGzwxService.selectDeviceFailureRepairByReportId(reportId));
        jsonObject.put("check", ncEmGcyzService.selectNcEmGcyzByReportId(reportId));
        return AjaxResult.success(jsonObject);
    }
}
